package com.web.controller.web;

import com.web.dao.*;
import com.web.domain.WebResponse;
import com.web.domain.dataTable.BootstrapTableColumns;
import com.web.entity.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;

import static com.web.manager.CoreManager.countTotalVal;

/**
 * @author FDjavaone
 * @date 2017/10/13
 */
@Slf4j
@Controller
public class WebPlatformController {

    @Autowired
    private PlatformMapper platformMapper;
    @Autowired
    private CompanyMarketDetailMapper companyMarketDetailMapper;
    @Autowired
    private CompanyFundDetailMapper companyFundDetailMapper;
    @Autowired
    private CompanyCompanyMsDetailMapper companyCompanyMsDetailMapper;
    @Autowired
    private CompanyReportMapper companyReportMapper;


    @RequestMapping(value = "sysm/transfer",
            method = RequestMethod.GET)
    public String transfer(Model model, HttpServletRequest request) {
        return "transfer";
    }


    @RequestMapping("/sysm/getPlatform")
    @ResponseBody
    public WebResponse getConfig() {
        Platform platform = platformMapper.selectByPrimaryKey(1L);
        return WebResponse.ok("操作成功", platform);
    }

    @Data
    public static class TransferColumns extends BootstrapTableColumns {
        private String bal1;
        private String bal2;
        private String bal3;
    }

    @RequestMapping("/sysm/companyToMarket")
    @ResponseBody
    public WebResponse companyToMarket(TransferColumns transferColumns) {
        Date date = new Date();
        Platform platform = platformMapper.selectByPrimaryKey(1L);
        if (platform.getCompanyFundPool().compareTo(new BigDecimal(transferColumns.getBal1())) == -1) {
            return WebResponse.fail("当前公司资金池可调配余额不足！");
        } else {
            CompanyReport companyReport = new CompanyReport();

            companyReport.setCreateTime(date);
            companyReport.setModifyTime(date);
            companyReport.setReserve1("公司资金池调入市场资金池");

            BigDecimal companyRecord = new BigDecimal("0");
            BigDecimal marketRecord = new BigDecimal("0");

            companyReport.setCompanyFund("-" + transferColumns.getBal1());
            companyRecord = new BigDecimal(companyReport.getCompanyFund());
            companyReport.setCompanyFund((companyReport.getCompanyFund() != null ? companyReport.getCompanyFund() : "") + "\r\n=" + platformMapper.selectByPrimaryKey(1L).getCompanyFundPool().add(companyRecord));

            companyReport.setMarketFund("+" + transferColumns.getBal1());
            marketRecord = new BigDecimal(companyReport.getMarketFund());
            companyReport.setMarketFund((companyReport.getMarketFund() != null ? companyReport.getMarketFund() : "") + "\r\n=" + platformMapper.selectByPrimaryKey(1L).getMarketFundPool().add(marketRecord));

            companyReport.setTotal("0\r\n=" + countTotalVal(companyReport));

            companyReportMapper.insertSelective(companyReport);
            //公司资金池减少，市场资金池增加
            platformMapper.companyFundToMarket(new BigDecimal(transferColumns.getBal1()), 1L);

            CompanyFundDetail companyFundDetail = new CompanyFundDetail();
            companyFundDetail.setCreateTime(date);
            companyFundDetail.setModifyTime(date);
            companyFundDetail.setContent("公司资金池调配至市场资金池，公司资金池减少");
            companyFundDetail.setDetail("-" + transferColumns.getBal1());
            companyFundDetail.setBal(platformMapper.selectByPrimaryKey(1L).getCompanyFundPool().toString());
            companyFundDetail.setDescription("公司资金池调配至市场资金池，公司资金池减少");
            companyFundDetail.setFromNum("");
            companyFundDetail.setName("后台");
            companyFundDetail.setPhone("");
            companyFundDetailMapper.insertSelective(companyFundDetail);


            CompanyMarketDetail companyMarketDetail = new CompanyMarketDetail();
            companyMarketDetail.setCreateTime(date);
            companyMarketDetail.setModifyTime(date);
            companyMarketDetail.setContent("公司资金池调配至市场资金池，市场资金池增加");
            companyMarketDetail.setDetail("+" + transferColumns.getBal1());
            companyMarketDetail.setBal(platformMapper.selectByPrimaryKey(1L).getMarketFundPool().toString());
            companyMarketDetail.setDescription("公司资金池调配至市场资金池，市场资金池增加");
            companyMarketDetail.setFromNum("");
            companyMarketDetail.setName("后台");
            companyMarketDetail.setPhone("");
            companyMarketDetailMapper.insertSelective(companyMarketDetail);

            return WebResponse.ok("操作成功", platform);
        }

    }


    @RequestMapping("/sysm/marketToCompany")
    @ResponseBody
    public WebResponse marketToCompany(TransferColumns transferColumns) {
        Date date = new Date();
        Platform platform = platformMapper.selectByPrimaryKey(1L);
        if (platform.getMarketFundPool().compareTo(new BigDecimal(transferColumns.getBal2())) == -1) {
            return WebResponse.fail("当前市场资金池可调配余额不足！");
        } else {

            CompanyReport companyReport = new CompanyReport();

            companyReport.setCreateTime(date);
            companyReport.setModifyTime(date);
            companyReport.setReserve1("市场资金池调入公司资金池");

            BigDecimal companyRecord = new BigDecimal("0");
            BigDecimal marketRecord = new BigDecimal("0");

            companyReport.setCompanyFund("+" + transferColumns.getBal2());
            companyRecord = new BigDecimal(companyReport.getCompanyFund());
            companyReport.setCompanyFund((companyReport.getCompanyFund() != null ? companyReport.getCompanyFund() : "") + "\r\n=" + platformMapper.selectByPrimaryKey(1L).getCompanyFundPool().add(companyRecord));

            companyReport.setMarketFund("-" + transferColumns.getBal2());
            marketRecord = new BigDecimal(companyReport.getMarketFund());
            companyReport.setMarketFund((companyReport.getMarketFund() != null ? companyReport.getMarketFund() : "") + "\r\n=" + platformMapper.selectByPrimaryKey(1L).getMarketFundPool().add(marketRecord));

            companyReport.setTotal("0\r\n=" + countTotalVal(companyReport));

            companyReportMapper.insertSelective(companyReport);

            //公司资金池增加，市场资金池减少
            platformMapper.marketFundToCompany(new BigDecimal(transferColumns.getBal2()), 1L);

            CompanyFundDetail companyFundDetail = new CompanyFundDetail();
            companyFundDetail.setCreateTime(date);
            companyFundDetail.setModifyTime(date);
            companyFundDetail.setContent("市场资金池调配至公司资金池，公司资金池增加");
            companyFundDetail.setDetail("+" + transferColumns.getBal2());
            companyFundDetail.setBal(platformMapper.selectByPrimaryKey(1L).getCompanyFundPool().toString());
            companyFundDetail.setDescription("市场资金池调配至公司资金池，公司资金池增加");
            companyFundDetail.setFromNum("");
            companyFundDetail.setName("后台");
            companyFundDetail.setPhone("");
            companyFundDetailMapper.insertSelective(companyFundDetail);


            CompanyMarketDetail companyMarketDetail = new CompanyMarketDetail();
            companyMarketDetail.setCreateTime(date);
            companyMarketDetail.setModifyTime(date);
            companyMarketDetail.setContent("市场资金池调配至公司资金池，市场资金池减少");
            companyMarketDetail.setDetail("-" + transferColumns.getBal2());
            companyMarketDetail.setBal(platformMapper.selectByPrimaryKey(1L).getMarketFundPool().toString());
            companyMarketDetail.setDescription("市场资金池调配至公司资金池，市场资金池减少");
            companyMarketDetail.setFromNum("");
            companyMarketDetail.setName("后台");
            companyMarketDetail.setPhone("");
            companyMarketDetailMapper.insertSelective(companyMarketDetail);

            return WebResponse.ok("操作成功", platform);
        }

    }


    @RequestMapping("/sysm/companyMsA")
    @ResponseBody
    public WebResponse companyMsA(TransferColumns transferColumns) {
        Date date = new Date();
        Platform platform = platformMapper.selectByPrimaryKey(1L);

        //公司ms增加
        platformMapper.companyMskAdd(new BigDecimal(transferColumns.getBal3()), 1L);

        CompanyCompanyMsDetail companyCompanyMsDetail = new CompanyCompanyMsDetail();
        companyCompanyMsDetail.setCreateTime(date);
        companyCompanyMsDetail.setModifyTime(date);
        companyCompanyMsDetail.setContent("后台调整注入MS，公司MS增加");
        companyCompanyMsDetail.setDetail("+" + transferColumns.getBal3());
        companyCompanyMsDetail.setBal(platformMapper.selectByPrimaryKey(1L).getCompanyMsk().toString());
        companyCompanyMsDetail.setDescription("后台调整注入MS，公司MS增加");
        companyCompanyMsDetail.setFromNum("");
        companyCompanyMsDetail.setName("后台");
        companyCompanyMsDetail.setPhone("");
        companyCompanyMsDetailMapper.insertSelective(companyCompanyMsDetail);

        return WebResponse.ok("操作成功", platform);
    }


    @RequestMapping("/sysm/companyMsS")
    @ResponseBody
    public WebResponse companyMsS(TransferColumns transferColumns) {
        Date date = new Date();
        Platform platform = platformMapper.selectByPrimaryKey(1L);
        if (platform.getCompanyMsk().compareTo(new BigDecimal(transferColumns.getBal3())) == -1) {
            return WebResponse.fail("当前公司MS可调配余额不足！");
        } else {
            //公司ms減少
            platformMapper.companyMskSubtract(new BigDecimal(transferColumns.getBal3()), 1L);

            CompanyCompanyMsDetail companyCompanyMsDetail = new CompanyCompanyMsDetail();
            companyCompanyMsDetail.setCreateTime(date);
            companyCompanyMsDetail.setModifyTime(date);
            companyCompanyMsDetail.setContent("后台调整減少MS，公司MS減少");
            companyCompanyMsDetail.setDetail("-" + transferColumns.getBal3());
            companyCompanyMsDetail.setBal(platformMapper.selectByPrimaryKey(1L).getCompanyMsk().toString());
            companyCompanyMsDetail.setDescription("后台调整減少MS，公司MS減少");
            companyCompanyMsDetail.setFromNum("");
            companyCompanyMsDetail.setName("后台");
            companyCompanyMsDetail.setPhone("");
            companyCompanyMsDetailMapper.insertSelective(companyCompanyMsDetail);

            return WebResponse.ok("操作成功", platform);
        }

    }

}
